<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <el-card shadow="always">
          <org-tree ref="orgTree" modelType="'dept','community','floor','unit','room'" @nodeClick="handleNodeClick" @firstNodeClick="handleFirstNodeClick"/>
        </el-card>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-card shadow="always">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="所属单元" prop="unitId">
        <el-select v-model="queryParams.unitId" placeholder="请选择所属单元">
          <el-option
            v-for="dict in unitOptions"
            :key="dict.id"
            :label="dict.unitName"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="投诉类型" prop="repairsType">
        <el-select v-model="queryParams.repairsType" placeholder="请选择投诉类型" clearable size="small">
          <el-option
            v-for="dict in repairsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="投诉人" prop="repairsPerson">
        <el-input
          v-model="queryParams.repairsPerson"
          placeholder="请输入投诉人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉状态" prop="repairsState">
        <el-select v-model="queryParams.repairsState" placeholder="请选择投诉状态" clearable size="small">
          <el-option
            v-for="dict in repairsStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['waitress:repairs:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['waitress:repairs:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['waitress:repairs:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['waitress:repairs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="投诉单号" align="center" prop="repairsOdd" />
      <el-table-column label="所属单元" align="center" prop="unitName" />
      <el-table-column label="所属房屋" align="center" prop="roomName" />
      <!--<el-table-column label="所属物业" align="center" prop="deptName" width="180"/>-->
      <el-table-column label="投诉类型" align="center" prop="repairsType" :formatter="repairsTypeFormat" />
      <el-table-column label="投诉人" align="center" prop="ownerName" />
      <el-table-column label="投诉电话" align="center" prop="repairsPhone" width="120"/>
      <el-table-column label="投诉内容" align="center" prop="repairsContent" show-overflow-tooltip/>
      <el-table-column label="投诉照片" align="center" prop="repairsImgs">
        <template slot-scope="scope" >
          <el-image
            v-if="scope.row.repairsImgs!=null"
            class="elimg"
            style="width: 50px; height: 50px"
            :src="scope.row.repairsImgs|splitImgSrcFirst"
            :preview-src-list="scope.row.repairsImgs|splitImgSrc"
            :z-index="9999"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="投诉状态" align="center" prop="repairsState" :formatter="repairsStateFormat"/>
      <el-table-column label="处理人" align="center" prop="resultsPerson" />
      <el-table-column label="处理结果" align="center" prop="results" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.repairsState == 10001"
                     size="mini"
                     type="text"
                     icon="el-icon-plus"
                     @click="generatorRepairOrder(scope.row)"
          >处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
        </el-card>
      </el-col>
    </el-row>
    <!-- 添加或修改投诉建议对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10">
          <fieldset>
            <legend>投诉/建议</legend>
            <el-col :span="8" :xs="24">
              <el-form-item label="所属小区" prop="communityId" >
                <el-select v-model="form.communityId" style="width: 100%;" placeholder="请选择所属小区" :disabled="tsAndJy">
                  <el-option
                    v-for="dict in communityOptions"
                    :key="dict.id"
                    :label="dict.communityName"
                    :value="dict.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="所属单元" prop="unitId">
                <el-select v-model="form.unitId" style="width: 100%;" placeholder="请选择所属单元"  :disabled="tsAndJy">
                  <el-option
                    v-for="dict in unitOptions"
                    :key="dict.id"
                    :label="dict.unitName"
                    :value="dict.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="所属房屋" prop="roomId">
                <el-select v-model="form.roomId" placeholder="请选择所属房屋"  :disabled="tsAndJy">
                  <el-option
                    v-for="dict in roomOptions"
                    :key="dict.id"
                    :label="dict.roomName"
                    :value="dict.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="投诉类型" prop="repairsType">
                <el-select v-model="form.repairsType" placeholder="请选择投诉类型"  :disabled="tsAndJy">
                  <el-option
                    v-for="dict in repairsTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="投诉人" prop="repairsPerson">
                <el-select v-model="form.repairsPerson" placeholder="请选择投诉人"  :disabled="tsAndJy">
                  <el-option
                    v-for="item in ownerOptions"
                    :key="item.customId"
                    :label="item.customName"
                    :value="item.customId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="投诉电话" prop="repairsPhone">
                <el-input v-model="form.repairsPhone" placeholder="请输入投诉电话"  :disabled="tsAndJy"/>
              </el-form-item>
            </el-col>
            <el-col :span="24" :xs="24">
              <el-form-item label="投诉内容" prop="repairsContent">
                <el-input v-model="form.repairsContent" type="textarea" placeholder="请输入内容"  :disabled="tsAndJy"/>
              </el-form-item>
            </el-col>
        <!--    <el-col :span="12" :xs="24">
              <el-form-item label="服务类型" prop="serviceType">
                <treeselect
                  :show-count="true"
                  :default-expand-level="1"
                  disable-branch-nodes="true"
                  v-model="form.serviceType"
                  :options="serviceDictOptions"
                  :normalizer="normalizer"
                  placeholder="请选择服务类型" />
              </el-form-item>
            </el-col>-->
            <el-col :span="24" :xs="24">
              <el-form-item label="投诉照片">
                <ImageUpload
                  v-model="form.repairsImgs"
                  :disabled="tsAndJy"
                  @handleUploadSuccess="(row)=>this.form.repairsImgs = row.fileInfoPath"
                  @removeImage="(row)=>this.form.repairsImgs = ''"
                />
              </el-form-item>
            </el-col>
          </fieldset>

          <fieldset v-if="repairsCl">
            <legend>处理</legend>
            <el-row :gutter="10">
              <el-col :span="8" :xs="24">
                <el-form-item label="投诉状态" prop="repairsState">
                  <el-select v-model="form.repairsState" placeholder="请选择投诉状态">
                    <el-option
                      v-for="dict in repairsStateOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8" :xs="24">
                <el-form-item label="处理人" prop="resultsPerson">
                  <el-input v-model="form.resultsPerson" placeholder="请输入处理人" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-col :span="24" :xs="24">
              <el-form-item label="处理结果" prop="results">
                <el-input v-model="form.results" type="textarea" placeholder="请输入处理结果" />
              </el-form-item>
            </el-col>
            <el-col ::span="24" :xs="24">
              <el-form-item label="处理图片">
                <ImageUpload
                  v-model="form.resultsImgs"
                  @handleUploadSuccess="(row)=>this.form.resultsImgs = row.fileInfoPath"
                  @removeImage="(row)=>this.form.resultsImgs = ''"
                />
              </el-form-item>
            </el-col>
          </fieldset>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listRepairs, getRepairs, delRepairs, addRepairs, updateRepairs, exportRepairs } from "@/api/waitress/repairs";
  import {addRepairOrder} from "@/api/property/repairOrder"
  import { listUnit,getUnit} from "@/api/property/unit";
  import {listServiceDict} from "@/api/system/serviceDict"
  import Treeselect from "@riophae/vue-treeselect";
  import {listOwnerRoom} from "@/api/property/ownerRoom"
  import { listCommunity,getCommunity} from "@/api/property/community";
  import { listRoom, getRoom} from "@/api/property/room";
  import { roomCustomList} from "@/api/waitress/customInfo";
  export default {
    name: "Repairs",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        repairsCl:false,
        tsAndJy:false,
        // 总条数
        total: 0,
        // 投诉建议表格数据
        repairsList: [],
        unitOptions:[],
        roomOptions:[],
        ownerOptions:[],
        serviceDictOptions: [],
        communityOptions:[],
        currentNode:{},
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 投诉类型字典
        repairsTypeOptions: [],
        // 投诉状态字典
        repairsStateOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          unitId: null,
          roomId: null,
          repairsType: null,
          deptId: null,
          repairsPerson: null,
          repairsPhone: null,
          repairsContent: null,
          serviceType: null,
          serviceDetailType: null,
          repairsState: null,
          resultsPerson: null,
          nodeId:null,
          communityId: null,
          nodeId:null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          communityId: [
            { required: true, message: "所属小区不能为空", trigger: "blur" }
          ],
          unitId: [
            { required: true, message: "所属单元不能为空", trigger: "blur" }
          ],
          roomId: [
            { required: true, message: "所属房屋不能为空", trigger: "blur" }
          ],
          repairsType: [
            { required: true, message: "投诉类型不能为空", trigger: "blur" }
          ],
        },
        repairOrderForm:{
          repairsId: null,
          unitId: null,
          repairContent: null,
          repairPerson: null,
          serviceType: null
        }
      };
    },
    components:{
      Treeselect
    },
    created() {
      //this.getList();
      this.getDicts("repairs_type").then(response => {
        this.repairsTypeOptions = response.data;
      });
      this.getDicts("repairs_state").then(response => {
        this.repairsStateOptions = response.data;
      });
      this.getunitList();
      this.communityList();
    },
    watch:{
      'form.unitId'(v){
        this.getRoomList();
      },
      'form.roomId'(v){
       this.getOwnerList()
      },
      'form.communityId'(v){
        this.getunitList()
      }
    },
    methods: {
      /** 查询投诉建议列表 */
      getList() {
        this.loading = true;
        listRepairs(this.queryParams).then(response => {
          this.repairsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      communityList(){
        listCommunity().then(response => {
          this.communityOptions = response.rows;
        })
      },
      getunitList(){
        listUnit({'communityId':this.form.communityId}).then(response => {
            this.unitOptions=response.rows;
        })
      },
      /** 转换服务类型数据字典数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.serviceCode,
          label: node.serviceName,
          children: node.children
        };
      },
      getRoomList(){
        if (this.form.unitId){
          listRoom({'unitId':this.form.unitId}).then(response => {
              this.roomOptions =  response.rows;
            let index = this.roomOptions.findIndex(x=>x.id == this.form.roomId)
            if(index == -1){
              this.form.roomId = '';
            }
          })
        }else {
          this.form.roomId = '';
        }
      },
      getOwnerList(){
        roomCustomList({'roomId':this.form.roomId}).then(response => {
          this.ownerOptions = response.data;
        })
      },
      // 投诉类型字典翻译
      repairsTypeFormat(row, column) {
        return this.selectDictLabel(this.repairsTypeOptions, row.repairsType);
      },
      // 投诉状态字典翻译
      repairsStateFormat(row, column) {
        return this.selectDictLabel(this.repairsStateOptions, row.repairsState);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          unitId: null,
          roomId: null,
          repairsType:null,
          deptId: null,
          repairsPerson: null,
          repairsPhone: null,
          repairsContent: null,
          repairsState: '10001',
          createBy: null,
          createDate: null,
          updateBy: null,
          updateDate: null,
          repairsOdd: null,
          repairsImgs: null,
          results: null,
          resultsImgs: null,
          resultsPerson: null,
          serviceType: null,
          communityId: null,
          hasApplyRepair:null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getTreeselect();
        if(this.currentNode.modelType == 'community'){
          this.form.communityId = this.currentNode.id
          this.open = true;
        }else if(this.currentNode.modelType == 'unit'){
          getUnit(this.currentNode.id).then(res=>{
            this.form.communityId = res.data.communityId
            this.form.unitId = res.data.id
            this.open = true;
          })
        }else if (this.currentNode.modelType == 'room'){
          getRoom(this.currentNode.id).then(res=>{
            this.form.communityId = res.data.communityId
            this.form.unitId = res.data.unitId
            this.form.roomId = res.data.id
            this.open = true;
          })
        }else {
          this.open = true;
        }
        this.repairsCl=false;
        this.tsAndJy=false;
        this.title = "添加投诉建议";
        this.getOwnerList();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        this.getOwnerList();
        const id = row.id || this.ids
        getRepairs(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.repairsCl=false;
          this.tsAndJy=false;
          this.title = "修改投诉建议";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRepairs(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRepairs(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 查询服务类型数据字典下拉树结构 */
      getTreeselect() {
        listServiceDict().then(response => {
          this.serviceDictOptions = [];
          const data = {serviceCode: 0, serviceName: '顶级节点', children: [] };
          data.children = this.handleTree(response.data, "serviceCode", "parentCode");
          this.serviceDictOptions.push(data);
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除投诉建议编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRepairs(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有投诉建议数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRepairs(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      },
      handleUpload(row){
        this.form.repairsImgs = row.fileInfoPath;
        this.form.resultsImgs=row.fileInfoPath;
      },
      removeImg(){
        this.form.repairsImgs = '';
        this.form.resultsImgs = '';
      },
      //生成报修单
      generatorRepairOrder(row){
/*        this.repairOrderForm.communityId = row.communityId
        this.repairOrderForm.repairsId = row.id;
        this.repairOrderForm.unitId = row.unitId;
        this.repairOrderForm.serviceType = row.serviceType;
        this.repairOrderForm.roomId = row.roomId;
        this.repairOrderForm.repairContent = row.repairsContent;
        this.repairOrderForm.repairPerson = row.repairsPerson
        addRepairOrder(this.repairOrderForm).then(res => {
          if(res.code === 200){
            this.$message.success(res.msg);
            this.getList();
          }
        })*/
        const id = row.id || this.ids
        getRepairs(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.repairsCl=true;
          this.tsAndJy=true;
          this.form.repairsState='10002'
          this.title = "处理投诉建议";
        });
      },
      handleNodeClick(node){
        this.currentNode = node;
        this.queryParams.nodeId = node.id
        this.getList()
      },
      handleFirstNodeClick(node){
        this.currentNode = node;
        this.queryParams.nodeId = node.id;
        this.getList();
      },
    }
  };
</script>

<style type="text/css">
  fieldset{
    margin-bottom: 20px;
  }
</style>
